﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BTCe.Data;


namespace BitcoinArbitrage
{
    public partial class BTCeTestFrame : Form
    {
        BTCeController _controller;

        public BTCeTestFrame()
        {
            InitializeComponent();

            _controller = new BTCeController();

            Initialized();
        }

        private void Initialized()
        {
            //로그인 여부 확인
            CheckLogin();
        }

        private void btnAccess_Click(object sender, EventArgs e)
        {
            _controller.Login();

            CheckLogin();
        }

        private void CheckLogin()
        {
            if (_controller.lsLogin())
            {
                Log(" 로그인 되어 있습니다.");
            }
            else
            {
                Log(" 로그아웃 되어 있습니다.");
            }
        }
        
        private void txtOutput_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnAccountInfo_Click(object sender, EventArgs e)
        {
            //WalletInfo wallet = _controller.GetWalletInfo();
            //Log("BTC 주소 :  " + wallet.In_BTC_Address);
            //Log("입금 은행 : " + wallet.In_KRW_BankName);
            
            WalletDeposit deposit = _controller.GetWalletDeposit();
            Log("전체 USD 금액 : " + deposit.Total_USD.ToString());
            Log("전체 RUR 금액 : " + deposit.Total_RUR.ToString());
            Log("전체 EUR 금액 : " + deposit.Total_EUR.ToString());
            Log("전체 BTC 금액 : " + deposit.Total_BTC.ToString());
        }

        private void Log(string strMessage)
        {
            txtOutput.Text = txtOutput.Text + strMessage + "\r\n";
        }

        private void btnOpenOrder_Click(object sender, EventArgs e)
        {
            _controller.RetreivedContract();

            dgvKorbitBid.DataSource = _controller.GetBidTable();

            dgvKorbitAsk.DataSource = _controller.GetAskTable();
        }

        private void dgvKorbitAsk_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (dgvKorbitAsk.Rows.Count > 1)
            {
                dgvKorbitAsk.FirstDisplayedScrollingRowIndex = dgvKorbitAsk.Rows.Count - 1;
            }

            dgvKorbitAsk.Sort(dgvKorbitAsk.Columns["BitcoinPrice"], ListSortDirection.Descending);
       }

        private void dgvKorbitBid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dgvOpenOrder_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dgvOpenOrder.Sort(dgvOpenOrder.Columns["timespan"], ListSortDirection.Descending);
        }

        private void dgvKorbitBid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //dgvKorbitBid.Sort(dgvKorbitBid.Columns["BitcoinPrice"], ListSortDirection.Descending);
        }

        private void btnOrderList_Click(object sender, EventArgs e)
        {
            _controller.RetreivedOpenOrder();

            dgvOpenOrder.DataSource = _controller.GetOpenOrderTable();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime d = new DateTime(Int64.Parse("1425364742000"));
            TimeSpan s = new TimeSpan(Int64.Parse("1425364742000"));
            Console.WriteLine(DateTime.Parse("1425364742000"));
        }

        private void btnBUY_Click(object sender, EventArgs e)
        {
            string strKRWPrice;
            string strBTCVolume;
            string strMarketPrice;

            strKRWPrice = txtKRWPrice.Text;
            strBTCVolume = txtBTCVolume.Text;
            strMarketPrice = txtMarketPrice.Text;

            if (cmbOrderType.Text == "시장가")
            {
                _controller.OrderBuyMarket(strMarketPrice);
            }
            else
            {
                _controller.OrderBuyLimit(strKRWPrice, strBTCVolume);
            }
        }

        private void btnSell_Click(object sender, EventArgs e)
        {
            string strKRWPrice;
            string strBTCVolume;
            string strMarketPrice;

            strKRWPrice = txtKRWPrice.Text;
            strBTCVolume = txtBTCVolume.Text;
            strMarketPrice = txtMarketPrice.Text;

            if (cmbOrderType.Text == "시장가")
            {
                _controller.OrderSellMarket(strBTCVolume);
            }
            else
            {
                _controller.OrderSellLimit(strKRWPrice, strBTCVolume);
            }
        }

        private void dgvOpenOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string strOrderID;

            strOrderID = (string)dgvOpenOrder.Rows[e.RowIndex].Cells["OrderID"].Value.ToString();

            txtOrderID.Text = strOrderID;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            string strOrderID;

            strOrderID = txtOrderID.Text;

            _controller.OrderCancel(strOrderID);
        }




    }
}
